﻿using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.Util;
using Kingdee.BOS.WebApi.FormService;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RK.SerivesPlugin.BPW
{
    [Description("保存成功后，根据条件自动提交出口确认单(销售订单)"), HotUpdate]
    public class SalConfirmAfterSave : AbstractOperationServicePlugIn
    {
        public override void AfterExecuteOperationTransaction(AfterExecuteOperationTransaction e)
        {
            base.AfterExecuteOperationTransaction(e);
            var obj = e.DataEntitys.FirstOrDefault();
            if (obj == null)
            {
                return;
            }
            DynamicObjectCollection rows = obj["FEntity"] as DynamicObjectCollection;
            string salBillNo = Convert.ToString(rows[0]["F_XRIS_SalNO"]);
            int rCount = rows.Count;
            //校验是否全部填写可交货日期
            var isUp = GetUpRows(salBillNo, rCount);
            if (isUp)
            {
                //提交销售订单
                SubitSalNo(salBillNo);
            }
        }

        private void SubitSalNo(string salBillNo)
        {
            var opResult = JsonConvert.SerializeObject(WebApiServiceCall.Submit(this.Context, "SAL_SaleOrder", Base.GetParam(salBillNo)));
        }
        /// <summary>
        /// 校验是否全部填写可交货日期
        /// </summary>
        /// <param name="salBillNo"></param>
        /// <param name="rCount"></param>
        /// <returns></returns>
        private bool GetUpRows(string salBillNo, int rCount)
        {
            var allRows = WebApiServiceCall.ExecuteBillQuery(this.Context, Base.QueryParameter("SAL_SaleOrder", "F_XRIS_TempDate", $"fbillno='{salBillNo}'"));
            var rows = WebApiServiceCall.ExecuteBillQuery(this.Context, Base.QueryParameter("SAL_SaleOrder", "F_XRIS_TempDate", $"F_XRIS_TempDate is null and fbillno='{salBillNo}'"));
            bool isUpRows = false;
            if (allRows.Count == rCount)
            {
                isUpRows = true;
            }
            else
            {
                if (rows.Count == 0)
                {
                    isUpRows = true;
                }
                else
                {
                    isUpRows = false;
                }
            }
            return isUpRows;
        }

        public override void OnPreparePropertys(PreparePropertysEventArgs e)
        {
            base.OnPreparePropertys(e);
            //加载所有字段
            FormMetadata meta = MetaDataServiceHelper.GetFormMetaData(this.Context, this.BusinessInfo.GetForm().Id);
            var fieldKeyList = meta.BusinessInfo.GetBosFields().Select(x => x.Field.Key).ToList();
            for (int i = 0; i < fieldKeyList.Count; i++)
            {
                e.FieldKeys.Add(fieldKeyList[i]);
            }
        }
    }
}
